package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.work.enums.DocumentsDeleteFlag;
import com.jiantong.work.impl.dao.ButcherReportDao;
import com.jiantong.work.model.ButcherReport;
import com.jiantong.work.service.ButcherReportReadService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by niufei on 2017/3/22.
 *
 */
@Slf4j
@Service
@RpcProvider
public class ButcherReportReadServiceImpl implements ButcherReportReadService{
    private final ButcherReportDao butcherReportDao;

    @Autowired
    public ButcherReportReadServiceImpl(ButcherReportDao butcherReportDao) {
        this.butcherReportDao = butcherReportDao;
    }

    @Override
    public Response<ButcherReport> findById(Long id) {
        try{
            ButcherReport butcherReport = butcherReportDao.findById(id);
            if(butcherReport == null){
                log.error("butcherReport(id={}) is not found", id);
                return Response.fail("butcherReport.not.found");
            }
            return Response.ok(butcherReport);
        }catch (Exception e){
            log.error("failed to find butcherReport(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("butcherReport.find.fail");
        }
    }

    @Override
    public Response<Paging<ButcherReport>> pagingButcherReport(Integer pageNo, Integer pageSize,List<Integer> statuses) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            List<Long> butcherFactoryIds = null;
            LoginUser loginUser = UserUtil.getCurrentUser();
            Integer organizationType = loginUser.getOrganizationType();
            if(organizationType == 2){
                butcherFactoryIds = new ArrayList<>();
                butcherFactoryIds.add(loginUser.getOrganizationId());
            }else if(organizationType == 3){
                butcherFactoryIds = loginUser.getButcherFactoryIds();

            }

            if(butcherFactoryIds != null && !butcherFactoryIds.isEmpty()){
                criteria.put("butcherFactoryIds",butcherFactoryIds);
            }else {
                return Response.ok(new Paging<>(0L,new ArrayList<>()));
            }
            if(statuses != null){
                criteria.put("statuses",statuses);
            }
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(butcherReportDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging butcherReportDao, cause:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("butcherReportDao.paging.fail");
        }
    }

    @Override
    public Response<Map<String, Object>> findStatusById(Long id) {
        try{
            Integer status = butcherReportDao.findStatusById(id);
            Map<String , Object> map = Maps.newHashMap();
            map.put("status",status);
            return Response.ok(map);
        }catch (Exception e){
            log.error("failed to find status(id = {}),error={}",id,Throwables.getStackTraceAsString(e));
            return Response.fail("failed to find status");
        }
    }

    @Override
    public Response<Paging<ButcherReport>> pagingButcherReportAll(Integer pageNo, Integer pageSize, List<Integer> statuses) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            Map<String, Object> criteria = Maps.newHashMap();
            if(statuses!=null) {
                criteria.put("statuses",statuses);
            }
            criteria.put("deleteFlag", DocumentsDeleteFlag.NORMAL.value());
            return Response.ok(butcherReportDao.pagingButcherReportAll(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging ButcherReport all, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("all ButcherReport.paging.fail");
        }
    }
    @Override
    public Response<Paging<ButcherReport>> pagingCommon(Integer pageNo, Integer pageSize,Map criteria){
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            return Response.ok(butcherReportDao.pagingCommon(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging ButcherReport all, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("all ButcherReport.paging.fail");
        }
    }
    @Override
    public Response<Paging<ButcherReport>> pagingCert(Integer pageNo, Integer pageSize,Map criteria){
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            return Response.ok(butcherReportDao.pagingCert(pageInfo.getOffset(),pageInfo.getLimit(),criteria));
        }catch (Exception e){
            log.error("failed to paging ButcherReport all, cause:{}",Throwables.getStackTraceAsString(e));
            return Response.fail("all ButcherReport.paging.fail");
        }
    }
}
